package jmine.tec.hydra.services.excluir;

import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.HydraController;
import jmine.tec.hydra.HydraMessages;
import jmine.tec.hydra.dao.HydraSpecificationDAO;
import jmine.tec.hydra.domain.HydraSystem;
import jmine.tec.hydra.domain.specification.HydraSpecificationImpl;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.ServiceExecutionException;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Serviço de exclusão de uma especificação de integração.
 * 
 * @author piercio
 */
@ServiceImplementor(action = ActionsEnum.EXCLUIR)
public class HydraSpecificationService {

    public static final String ACTION = "Ação";

    public static final String SYSTEM = "Sistema";

    private String action;

    private HydraSystem system;

    protected HydraController hydraController;

    /**
     * Executa o serviço e remove um {@link HydraSpecificationImpl}
     * 
     * @throws ServiceExecutionException caso não encontre a especificação.
     */
    @Execution
    public void executeService() throws ServiceExecutionException {
        try {
            HydraSpecificationDAO dao = this.hydraController.getDAOFactory().getDAOByClass(HydraSpecificationDAO.class);
            HydraSpecificationImpl spec = dao.findByActionSystem(this.system, this.action);
            spec.getPersister().remove();
        } catch (BeanNotFoundException e) {
            throw new ServiceExecutionException(HydraMessages.SPECIFICATION_NOT_FOUND_SYSTEM_ACTION.create(this.system, this.action), e);
        }
    }

    /**
     * @param hydraController o hydraController
     */
    @Injected
    public void setHydraController(HydraController hydraController) {
        this.hydraController = hydraController;
    }

    /**
     * @param action a ação da especificação.
     */
    @Input(fieldName = ACTION)
    public void setAction(String action) {
        this.action = action;
    }

    /**
     * @param system o sistema da especificação.
     */
    @Input(fieldName = SYSTEM)
    public void setSystem(HydraSystem system) {
        this.system = system;
    }

}
